/**
 * 功能描述
 */

define(function (require, exports, module) {
	var Grid = require("grid");
	var aw = require("ajaxwrapper");
	var tools = require("tools");
	var sealStatus;
	var driverStatus;
    var instance = {
        initComponent: function (params,widget) {
        	var grid = new Grid({
        		parentNode: ".J-grid",
        		model:{
        			url:"api/driverfile/query",
        			params:function(){
        				var subnav = widget.get("subnav");
        				var status = subnav.getValue("status");
        				if(status=="false"){
        					sealStatus = status;
        				}else{
        					driverStatus = status;
        				};
        				return{
        					"seal":sealStatus,
        					"driverStatus":driverStatus,
//        					fetchProperties:"pkDriver,personalInfo.*,personalInfo.driverInfo.*,version"
        					fetchProperties:"*,personalInfo.*,personalInfo.driverInfo,personalInfo.driverInfo.driveBookType,personalInfo.driverInfo.driveBookDate"
//        									"personalInfo.driverInfo.*" 
        				}
        				
        			},
        			columns:[{
        				name:"personalInfo.name",
        				label:"姓名"
        			},{
        				name:"personalInfo.birthday",
        				format:"date",
        				label:"出生日期"
        			},{
        				name:"age",
        				label:"年龄",
        				format:function(value,row){
        					if(row.personalInfo.birthday){
        						return moment().format("YYYY")-moment(row.personalInfo.birthday).format("YYYY");
        					}else{
        						return " ";
        					}
        				}
        			},{
        				name:"personalInfo.idNumber",
        				label:"身份证号"
        			},{
        				name:"personalInfo.driverInfo.driveBookType.value",
        				label:"驾驶本类型"
        			},{
        				name:"personalInfo.driverInfo.driveBookDate",
        				format:"date",
        				label:"取得驾驶本日期"
        			},{
        				name:"driveBookAge",
        				label:"驾龄",
        				format:function(value,row){
        					if(row.personalInfo.driverInfo.driveBookDate){
        						return moment().format("YYYY")-moment(row.personalInfo.driverInfo.driveBookDate).format("YYYY");
        					}else{
        						return " ";
        					}
        				}
        			},{
        				name:"personalInfo.phone",
        				label:"联系电话"
        			},{
        				name:"personalInfo.address",
        				label:"联系地址"
        			},{
        				name:"driverStatus.value",
        				label:"状态",
        				format:function(value,row){
        					if(row.seal){
        						if(row.driverStatus){
        							return row.driverStatus.value;
        						}else{
        							return "停用";
        						}
        					}else{
        						return "停用";
        					}
        				}
        			},{
        				name:"operate",
        				label:"操作",
        				format:"button",
        				formatparams:[{
        					id:"open",
        					text:"启用",
        					show:function(index,data,rowEle){
        						var flag = false;
        						
        						if(data.seal==false){
        							flag = true;
        						}
        						if(data.driverStatus!=null&&(data.driverStatus.key=="Free" || data.driverStatus.key=="Passage")){
        							flag = false;
        						}
        						if(flag){
        							return true;
        						}
        						
							},
							handler:function(index,data,rowEle){
								var cParams = {
										pkDriver: data.pkDriver,
										version: data.version,
										driverStatus:"Free",
										seal: true,
									};
								aw.saveOrUpdate("api/driverfile/save",cParams,function(cParams){
									grid.refresh();
								});
							}
        				},{
							id:"close",
							text:"停用",
							show:function(index,data,rowEle){
								
								if(data.driverStatus!=null && (data.driverStatus.key=="Free")){
									return true;
								}else if(!data.seal){
									return false;
								}
							},
							handler:function(index,data,rowEle){
								var cParams = {
									pkDriver: data.pkDriver,
									version: data.version,
									driverStatus:" ",
									personalInfo:data.personalInfo.pkPersonalInfo,
									seal: false,
								};
							aw.saveOrUpdate("api/driverfile/save",cParams,function(cParams){
								grid.refresh();
							});
							}
						},{
        					id:"edit",
        					text:"修改",
        					show:function(index,data,rowEle){
								
        						if(data.driverStatus!=null &&(data.driverStatus.key=="Passage")){
        							return false;
        						} else {
        							return true;
        						}
        						
							},
        					handler:function(index,data,rowEle){
        						widget.get("subnav").hide(["search","add","status"]);	
        						form = widget.get("form");
        						form.setValue("pkDriver",data.pkDriver);
        						form.setValue("driverStatus",data.driverStatus.key);
        						form.setValue("version",data.version);
        						form.setValue("seal",data.seal);
        						form.setValue("name",data.personalInfo.name);
        						form.setValue("idNumber",data.personalInfo.idNumber);
        						form.setValue("birthday",moment(tools.getBirthdateByIdNo(data.personalInfo.idNumber)).format("YYYY-MM-DD"));
        						form.setValue("age",tools.getAgeByBirthdate(tools.getBirthdateByIdNo(data.personalInfo.idNumber)));
        						form.setValue("phone",data.personalInfo.phone);
        						form.setValue("address",data.personalInfo.address);
        						form.setValue("servicePoint",data.servicePoint.pkServicePoint);
        						form.setValue("driveBookType",data.personalInfo.driverInfo.driveBookType.key);
        						form.setValue("driveBookDate",moment(data.personalInfo.driverInfo.driveBookDate).format("YYYY-MM-DD"));
        						form.setValue("driveBookAge",tools.getAgeByBirthdate(moment(data.personalInfo.driverInfo.driveBookDate).format("YYYY-MM-DD")));
        						widget.hide([".J-grid"]).show([".J-form"]);
        					}
        				},{
        					id:"delete",
        					text:"删除",
        					show:function(index,data,rowEle){
        						var flag = false;
        						if(data.driverStatus!=null &&(data.driverStatus.key=="Free"||data.driverStatus.key=="Passage")){
        							flag = false;
        						}
        						if(!data.seal){
        							flag = true;
        						}
        						
        						if(flag){
        							return true;
        						}
							},
        					handler:function(index,data,rowEle){
								aw.del("api/driverfile/"+data.pkDriver+"/delete",function() {
									grid.refresh();
								});	
							}
        				}]
        			},]
        		},
        		
        	});
        	widget.set("grid",grid)
        }
    };
    module.exports = instance;
});
